package com.example.fresh_market.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.recyclerview.extensions.ListAdapter;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.example.fresh_market.R;
import com.example.fresh_market.entity.UserDO;
import com.example.fresh_market.utils.HttpUtil;

import org.apache.commons.lang3.StringUtils;

public class UserListAdapter extends ListAdapter<UserDO, UserListAdapter.ViewHolder> {

    private final OnItemClickListener onItemClickListener;
    private final Context mContext;

    public UserListAdapter(Context mContext, OnItemClickListener clickListener) {
        super(new DiffCallback());
        this.mContext = mContext;
        this.onItemClickListener = clickListener;
    }

    protected UserListAdapter(@NonNull DiffUtil.ItemCallback<UserDO> diffCallback,
                              Context mContext, OnItemClickListener clickListener) {
        super(diffCallback);
        this.mContext = mContext;
        this.onItemClickListener = clickListener;
    }

    public UserDO getItem(int position) {
        return super.getItem(position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new UserListAdapter.ViewHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.user_list_item, viewGroup, false));
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        UserDO userdo = getItem(i);
        viewHolder.bindBean(userdo);
        if (StringUtils.isNotBlank(userdo.getAvatar())) {
            RequestOptions requestOptions = new RequestOptions()
                    .override(120, 160);
            HttpUtil.loadImage(viewHolder.user_avatar, userdo.getAvatar(), requestOptions);
        } else {
            Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.logo);
            viewHolder.user_avatar.setImageDrawable(drawable);
        }
        viewHolder.user_edit.setOnClickListener(v -> {
            onItemClickListener.onEditClick(v, viewHolder.getAdapterPosition());
        });
        viewHolder.user_delete.setOnClickListener(v -> {
            onItemClickListener.onDeleteClick(v, viewHolder.getAdapterPosition());
        });
        viewHolder.user_status.setOnClickListener(v -> {
            onItemClickListener.onStatusClick(viewHolder.itemView, viewHolder.getAdapterPosition());
        });
    }

    public interface OnItemClickListener {
        void onEditClick(View view, int position);

        void onDeleteClick(View view, int position);

        void onStatusClick(View view, int position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView user_name;
        private final TextView nickname;
        private final TextView phone;
        private final TextView password;
        private final ImageView user_avatar;
        private final TextView email;
        private final Switch user_status;
        private final TextView user_edit;
        private final TextView user_delete;
        private final TextView address;

        ViewHolder(View item) {
            super(item);
            user_name = item.findViewById(R.id.user_name);
            nickname = item.findViewById(R.id.nickname);
            phone = item.findViewById(R.id.phone);
            password = item.findViewById(R.id.password);
            email = item.findViewById(R.id.email);
            address = item.findViewById(R.id.address);
            user_avatar = item.findViewById(R.id.user_avatar);
            user_status = item.findViewById(R.id.user_status);
            user_edit = item.findViewById(R.id.user_edit);
            user_delete = item.findViewById(R.id.user_delete);
        }

        void bindBean(final UserDO userDO) {
            user_name.setText(String.format("用户名:%s", userDO.getUsername()));
            nickname.setText(String.format("昵称:%s", userDO.getNickname()));
            password.setText(String.format("密码:%s", userDO.getPassword()));
            phone.setText(String.format("电话:%s", StringUtils.isBlank(userDO.getPhone()) ? "无" : userDO.getPhone()));
            email.setText(String.format("邮箱:%s", StringUtils.isBlank(userDO.getEmail()) ? "无" : userDO.getPhone()));
            address.setText(String.format("地址:%s", StringUtils.isBlank(userDO.getAddress()) ? "无" : userDO.getAddress()));
            user_status.setChecked(userDO.getStatus());
            user_status.setText(userDO.getStatus() ? "启用" : "禁用");
        }
    }

    private static class DiffCallback extends DiffUtil.ItemCallback<UserDO> {
        @Override
        public boolean areItemsTheSame(@NonNull UserDO oldItem, @NonNull UserDO newItem) {
            return oldItem.getId().equals(newItem.getId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull UserDO oldItem, @NonNull UserDO newItem) {
            return oldItem.equals(newItem);
        }
    }
}
